<?php
class Controller_WWW_Activation extends Controller_WWW_Template
{

    public function __construct ($req, $res)
    {
        $this->template = "smarty:www/account";
        parent::__construct($req, $res);
    }

    public function action_index(){
        $uid = 77;
        $user_info = User_Account::user_account_info($uid)->current();
        $this->template->set("user",$user_info);
        $this->template->set_filename('smarty:www/activation/active');
    }

    public function action_subscribe(){
        $post = $this->request->post();
        if(!empty($post)){
            User_Subscribe::user_activation($post['uid'],$post['email']);
        }
    }

    public function action_subscribe_update($uid){
        $post = $this->request->post();
        if(!empty($post)){
//            var_dump($post);
            //获取用户
            User_Subscribe::user_activation($post['uid'],$post['email']);

        }else{
            $this->template->set("uid",$uid);
            $this->template->set_filename('smarty:www/activation/edit');
        }
    }

    public function action_confirm(){
        $query = $this->request->query();
        $token = Arr::get($query,'token');
        $msg = $this->confirm_token($token);

        $this->template->set("msg",$msg);
        $this->template->set_filename('smarty:www/activation/active');
    }

    /**
     * @param $token
     * @return string
     */
    public function confirm_token($token)
    {
        $data = User_Account::account_checkout('userToken', $token)->current();
        if (empty($data)) {
            echo $msg = "验证失败";
            return $msg;
        } else {
            if ($data['active'] == 1) {
                $msg = "验证完成，无须再次验证";
//                $this->request->redirect("/activation/");
                return $msg;
            } else {
                if (time() > $data['token_exptime']) {
                    $msg = "邮件过期";
                    return $msg;
                } else {
                    echo $msg = "验证通过";
                    $save['active'] = 1;
                    User_Account::update($save, 'userToken', $token);
//                    $this->request->redirect("/activation/");
                    return $msg;
                }
            }
        }
    }


}
